package com.hjq.mallproduct.service.impl;

import com.alibaba.fastjson.JSON;
import com.hjq.common.entity.es.SkuEsModel;
import com.hjq.common.utils.R;
import com.hjq.mallproduct.entity.*;
import com.hjq.mallproduct.entity.to.SeckillInfoTO;
import com.hjq.mallproduct.entity.vo.ItemVO;
import com.hjq.mallproduct.entity.vo.SkuItemSaleAttrVO;
import com.hjq.mallproduct.entity.vo.SpuItemAttrGroupVO;
import com.hjq.mallproduct.feign.SeckillFeign;
import com.hjq.mallproduct.service.*;
import com.mysql.cj.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hjq.common.utils.PageUtils;
import com.hjq.common.utils.Query;

import com.hjq.mallproduct.dao.SkuInfoDao;


@Service("skuInfoService")
public class SkuInfoServiceImpl extends ServiceImpl<SkuInfoDao, SkuInfoEntity> implements SkuInfoService {
    @Autowired
    SkuImagesService imagesService;
    @Autowired
    SpuInfoDescService spuInfoDescService;
    @Autowired
    AttrGroupService attrGroupService;
    @Autowired
    SkuSaleAttrValueService saleAttrValueService;
    @Autowired
    SpuImagesService spuImagesService;
    @Autowired
    ThreadPoolExecutor threadPoolExecutor;
    @Autowired
    SeckillFeign seckillFeign;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params),
                new QueryWrapper<SkuInfoEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void saveSkuInfo(SkuInfoEntity skuInfoEntity) {
        this.save(skuInfoEntity);
    }

    @Override
    public PageUtils queryPageByCondition(Map<String, Object> params) {
        String key =(String)params.get("key");
        String brandId = (String)params.get("brandId");
        String catelogId = (String)params.get("catelogId");
        String max = (String)params.get("max");
        String min = (String)params.get("min");


        QueryWrapper<SkuInfoEntity> skuInfoEntityQueryWrapper = new QueryWrapper<>();
        if(!catelogId.equals("0") &&!StringUtils.isEmptyOrWhitespaceOnly(catelogId)){
            skuInfoEntityQueryWrapper.eq("catalog_id",catelogId);
        }
        if(!StringUtils.isEmptyOrWhitespaceOnly(brandId) && !brandId.equals("0")){
            skuInfoEntityQueryWrapper.eq("brand_id",brandId);
        }
        if(!StringUtils.isEmptyOrWhitespaceOnly(max)
                &&
                !StringUtils.isEmptyOrWhitespaceOnly(min)
        )
        {
            if(Integer.parseInt(max)>Integer.parseInt(min) && Integer.parseInt(min)>0) {
                skuInfoEntityQueryWrapper.between("price",min,max);
            }

        }
        if(!StringUtils.isEmptyOrWhitespaceOnly(key)){
            skuInfoEntityQueryWrapper.and((item)->item.eq("sku_id",key).or().like("sku_name",key).or().like("sku_desc",key));

        }

        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params),
                skuInfoEntityQueryWrapper
        );
        return new PageUtils(page);
    }

    @Override
    public void upSpuInfoToES(Long spuId) {

    }

    @Override
    public List<SkuInfoEntity> getSkusBySpuId(Long spuId) {
        return baseMapper.selectList(new QueryWrapper<SkuInfoEntity>().eq("spu_id",spuId));

    }

    @Override
    public ItemVO getItem(Long skuId) {
        ItemVO itemVO = new ItemVO();
        CompletableFuture<SkuInfoEntity> infoFuture = CompletableFuture.supplyAsync(() -> {
            //skuInfo基本信息 sku_info
            SkuInfoEntity skuInfoEntity = baseMapper.selectById(skuId);
            itemVO.setSkuInfo(skuInfoEntity);
            return skuInfoEntity;
        }, threadPoolExecutor);
        //spu销售属性组合
        CompletableFuture<Void> salesFuture = infoFuture.thenAcceptAsync((result) -> {
            List<SkuItemSaleAttrVO> saleAttrVOS = saleAttrValueService.getSaleAttrsBySpuId(result.getSpuId());
            itemVO.setSaleAttrVOS(saleAttrVOS);
        }, threadPoolExecutor);
        CompletableFuture<Void> descFuture = infoFuture.thenAcceptAsync((resultOf) -> {
            //spu介绍
            SpuInfoDescEntity descEntity = spuInfoDescService.getById(resultOf.getSpuId());
            itemVO.setDesc(descEntity);
        }, threadPoolExecutor);
        CompletableFuture<Void> picFuture = infoFuture.thenAcceptAsync((resultOf) -> {
            //spu图片
            List<SpuImagesEntity> spu_id = spuImagesService.list(new QueryWrapper<SpuImagesEntity>().eq("spu_id", resultOf.getSpuId()));
            itemVO.setSpuImagesEntity(spu_id);
        }, threadPoolExecutor);
        CompletableFuture<Void> paramFuture = infoFuture.thenAcceptAsync((resultOf) -> {
            //spu的规格参数
            List<SpuItemAttrGroupVO> groupVO = attrGroupService.getAttrGroupWithAttrBySpuId(resultOf.getSpuId(), resultOf.getCatalogId());
            itemVO.setGroupVOS(groupVO);
        }, threadPoolExecutor);
        //sku图片 sku_images
        CompletableFuture<Void> imageThread = CompletableFuture.runAsync(() -> {
            List<SkuImagesEntity> skuImages = imagesService.getImagesBySkuId(skuId);
            itemVO.setSkuImages(skuImages);
        }, threadPoolExecutor);
        //秒杀信息
        CompletableFuture<Void> secThread = CompletableFuture.runAsync(() -> {
            R skuSecInfo = seckillFeign.getSkuSecInfo(skuId);
            if(skuSecInfo.getCode()==0){
                String string = JSON.toJSONString(skuSecInfo.get("data"));
                SeckillInfoTO seckillInfoTO = JSON.parseObject(string, SeckillInfoTO.class);
                itemVO.setSeckillInfo(seckillInfoTO);
            }
        }, threadPoolExecutor);
        try {
            CompletableFuture.allOf(salesFuture, descFuture,picFuture,paramFuture,imageThread,secThread).get();
        } catch (Exception e){
        }
        return itemVO;
    }

    @Override
    public BigDecimal getPriceBySkuId(Long skuId) {
        List<SkuInfoEntity> sku_id = list(new QueryWrapper<SkuInfoEntity>().eq("sku_id", skuId));
        SkuInfoEntity skuInfoEntity = sku_id.get(0);
        return skuInfoEntity.getPrice();
    }


}